package com.tobby.camel.camelinaction.chapter5.errorhandler;

import org.apache.camel.Exchange;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.spring.CamelSpringTestSupport;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringRouteScopeTest extends CamelSpringTestSupport {

	@Override
	protected AbstractApplicationContext createApplicationContext() {
		return new ClassPathXmlApplicationContext(
				"com/tobby/camel/camelinaction/chapter5/errorhandler/RouteScopeTest.xml");
	}

	@Override
	public void setUp() throws Exception {
		deleteDirectory("target/orders");
		super.setUp();
	}

	@Test
	public void testOrderOK() throws InterruptedException {
		// we expect the file to be converted to csv and routed to the 2nd route
		MockEndpoint file = getMockEndpoint("mock:file");
		file.expectedMessageCount(1);

		// we expect the 2nd route to complete
		MockEndpoint mock = getMockEndpoint("mock:queue.order");
		mock.expectedBodiesReceived("amount=1,name=Camel in Action,id=123,status=OK");

		template.sendBodyAndHeader("file://target/orders",
				"amount=1#name=Camel in Action", Exchange.FILE_NAME, "order.txt");
		assertMockEndpointsSatisfied();
	}

	@Test
	public void testOrderActiveMQ() throws InterruptedException {
		// we expect the file to be converted to csv and routed to 2nd route
		MockEndpoint file = getMockEndpoint("mock:file");
		file.expectedMessageCount(1);

		// we do not expect the 2nd route to complete
		MockEndpoint mock = getMockEndpoint("mock:queue.order");
		mock.expectedMessageCount(0);

		template.sendBodyAndHeader("file://target/orders",
				"amount=1#name=ActiveMQ in Action", Exchange.FILE_NAME, "order.txt");

		// wait 10 seconds to let the test run
		Thread.sleep(10000);

		assertMockEndpointsSatisfied();
	}

	@Test
	public void testXmlOrderFail() throws InterruptedException {
		// we do not expect the file to be converted to csv
		MockEndpoint file = getMockEndpoint("mock:file");
		file.expectedMessageCount(0);

		// we do not expect the 2nd route to complete
		MockEndpoint mock = getMockEndpoint("mock:queue.order");
		mock.expectedMessageCount(0);

		template.sendBodyAndHeader("file://target/orders",
				"<?xml version=\"1.0\"?><order><amount>1</amount><name>Camel in Action</name></order>",
				Exchange.FILE_NAME, "order.txt");

		// wait 5 seconds to let the test run
		Thread.sleep(5000);

		assertMockEndpointsSatisfied();
	}

}
